<template>
  <div class="login-container">
    <el-card class="login-card" shadow="hover">
      <div class="card-content">
        <!-- 左侧用户信息 -->
        <div class="left-panel">
          <div class="user-info">
            <img src="@/assets/img1.jpg" alt="avatar" class="avatar" />
            <h3 class="username">Hypno-Shroom</h3>
          </div>
        </div>

        <!-- 右侧登录表单 -->
        <div class="right-panel">
          <div class="form-container">
            <h2 class="login-title">用户登录</h2>
            <el-form :model="form" ref="formRef" label-width="60px" class="login-form">
              <el-form-item label="账号">
                <el-input v-model="form.username" placeholder="请输入账号"></el-input>
              </el-form-item>

              <el-form-item label="密码">
                <el-input
                  v-model="form.password"
                  type="password"
                  placeholder="请输入密码"
                ></el-input>
              </el-form-item>

              <div class="button-group">
                <el-button type="primary" class="login-btn" @click="handleLogin">
                  登录
                </el-button>
                <el-button type="text" class="register-btn" @click="goRegister">
                  注册
                </el-button>
              </div>
            </el-form>
          </div>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { loginApi } from '@/api/auth'
import { useUserStore } from '@/store/user'

const router = useRouter()
const store = useUserStore()

const form = reactive({
  username: '',
  password: ''
})

const errorMsg = ref('')
const formRef = ref()

const handleLogin = async () => {
  errorMsg.value=''
  if(!formRef.value) return

  // 模拟验证
  if(!form.username || !form.password){
    errorMsg.value = '请输入用户名和密码'
    ElMessage.warning(errorMsg.value)
    return
  }
  try{
    const res: any = await loginApi(form)
    store.setUser(res.token, res.username)
    ElMessage.success('登录成功')
    router.push('/calendar')
  } catch (err: any) {
    errorMsg.value = err.message || '登录失败'
    ElMessage.error(errorMsg.value)
  }
}

const goRegister = () => {
  router.push('/register')
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  animation: fadeIn 1.2s ease;
}

.login-card {
  width: 750px;
  height: 350px;
  border-radius: 16px;
  overflow: hidden;
  border: none;
  background: linear-gradient(135deg, #f5f7ff 0%, #e8edff 100%);
}

.card-content {
  display: flex;
  height: 100%;
}

/* 左侧用户信息区域 */
.left-panel {
  flex: 1;
  background: linear-gradient(135deg, #f5f7ff 0%, #e8edff 100%);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  border-right: 2px solid #3f51b5;
  position: relative;
}

.left-panel::after {
  content: '';
  position: absolute;
  right: -1px;
  top: 50%;
  transform: translateY(-50%);
  width: 2px;
  height: 80%;
  background: linear-gradient(to bottom, transparent, #3f51b5, transparent);
}

.user-info {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.avatar {
  width: 120px;
  height: 120px;
  border-radius: 50%;
  object-fit: cover;
  border: 4px solid #3f51b5;
  margin-bottom: 20px;
  box-shadow: 0 4px 12px rgba(63, 81, 181, 0.3);
}

.username {
  font-size: 22px;
  font-weight: 600;
  color: #1a237e;
  margin: 0;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

/* 右侧登录区域 */
.right-panel {
  flex: 1.2;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #f5f7ff 0%, #e8edff 100%);
}

.form-container {
  width: 100%;
  padding: 40px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.login-title {
  color: #1a237e;
  font-size: 24px;
  font-weight: 600;
  margin-bottom: 30px;
  text-align: center;
}

.login-form {
  width: 100%;
  max-width: 280px;
}

.button-group {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 24px;
}

.login-btn {
  width: 70%;
  border-radius: 8px;
  height: 40px;
  font-size: 16px;
  background: linear-gradient(135deg, #3f51b5 0%, #283593 100%);
  border: none;
}

.login-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(63, 81, 181, 0.4);
  transition: all 0.3s ease;
}

.register-btn {
  color: #3f51b5;
  font-size: 14px;
  font-weight: 500;
}

.register-btn:hover {
  color: #283593;
  text-decoration: underline;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-card {
    width: 90%;
    height: auto;
    margin: 20px;
  }
  
  .card-content {
    flex-direction: column;
  }
  
  .left-panel {
    border-right: none;
    border-bottom: 2px solid #3f51b5;
    padding: 30px 0;
  }
  
  .left-panel::after {
    display: none;
  }
  
  .form-container {
    padding: 30px 20px;
  }
}
</style>